package org.weed.remote.service;

import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;

import org.weed.remote.ui.RemoteMonitor;

public class DeviceServiceImpl implements DeviceService {
	private Robot robot;
	private Rectangle screenSize;
	
	public DeviceServiceImpl() throws AWTException {
		robot = new Robot();
		screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
	}
	
	// ========== Mouse
	public void mouseMove(int x, int y) {
		robot.mouseMove(x, y);
	}
	
	public void mouseMoving(int xSpeed, int ySpeed) {
		//TODO
	}
	
	public void mousePress(int button) {
		switch (button) {
		case EventService.LEFT:
			robot.mousePress(InputEvent.BUTTON1_MASK);
			break;
		case EventService.RIGHT:
			robot.mousePress(InputEvent.BUTTON2_MASK);
			break;
		}
	}
	
	public void mouseRelease() {
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		robot.mouseRelease(InputEvent.BUTTON2_MASK);
	}
	
	public void mouseClick(int button) {
		switch (button) {
		case EventService.LEFT:
			robot.mousePress(InputEvent.BUTTON1_MASK);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);
			break;
		case EventService.RIGHT:
			robot.mousePress(InputEvent.BUTTON2_MASK);
			robot.mouseRelease(InputEvent.BUTTON2_MASK);
			break;
		}
	}
	
	public void mouseWheel(int distance) {
		//TODO
	}

	public void mouseWheeling(int speed) {
		//TODO
	}
	// ========== Keyboard
	public void keyPress(int key) {
		//TODO
	}
	
	public void keyRelease() {
		//TODO
	}
	
	public void keyClick(int key) {
		//TODO
	}
	
	// ========== Screen
	private GraphicsDevice[] getGraphicsDevices() {
		GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		return graphEnv.getScreenDevices();
	}
	
	private GraphicsDevice getGraphicsDevice(int index) {
		return getGraphicsDevices()[index];
	}
	
	public int getScreenCount() {
		return getGraphicsDevices().length;
	}
	
	public Rectangle getScreenSize() {
		Rectangle allBounds = new Rectangle();
		for (GraphicsDevice monitor : getGraphicsDevices()) {
			Rectangle bounds = monitor.getDefaultConfiguration().getBounds();
			allBounds.width += bounds.width;
			allBounds.height = Math.max(allBounds.height, bounds.height);
		}
		return allBounds;
	}
	
	public Rectangle getScreenSize(int index) {
		return getGraphicsDevice(index).getDefaultConfiguration().getBounds();
	}
	
	public BufferedImage screenShot() {
		//TODO
		return null;
	}
	
	public BufferedImage screenShot(int index) {
		BufferedImage screen = robot.createScreenCapture(screenSize);
		int w = screen.getWidth();
		int h = screen.getHeight();
		
		BufferedImage image = new BufferedImage(w, h, screen.getType());
		Graphics2D graphics = image.createGraphics();
		graphics.drawImage(screen, 0, 0, w, h, null);
		return image;
	}

	public BufferedImage screenShot(Rectangle screenBounds) {
		return robot.createScreenCapture(screenBounds);
	}
	
	
	
	//==============================================
	public static void main(String[] args) throws Exception {
		DeviceServiceImpl d = new DeviceServiceImpl();
		System.out.println("Screen count: " + d.getScreenCount());
		System.out.println("Screen bounds: " + d.getScreenSize());
		for (int i=0; i< d.getScreenCount(); i++) {
			System.out.println("Screen bounds: " + d.getScreenSize(i));
		}
		Rectangle screenSize = d.getScreenSize(1);
		RemoteMonitor monitor = new RemoteMonitor(screenSize);
		
		int scale = 2;
		do {
			BufferedImage screenShot = d.screenShot(screenSize);
			Image screen = screenShot.getScaledInstance(screenSize.width/scale, screenSize.height/scale, BufferedImage.SCALE_FAST);
			BufferedImage screenShot2 = new BufferedImage(screenShot.getWidth(null)/scale, screenShot.getHeight(null)/scale, screenShot.getType());
			Graphics2D graph = screenShot2.createGraphics();
			//graph.scale(1/scale, 1/scale);
			graph.drawImage(screenShot, 0, 0, null);
			graph.dispose();

			
						
			monitor.refresh(screen);
		} while (false);
		
		
		
	}
}
